#include<iostream>
#include"ProblemA.h"
#include"cmath"
using namespace std;

//V=L[0.5\pi r^2 - r^2 \arcsin(h/r) -h(r^2-h^2)^(1/2)]
//L=10ft, r=1ft, V=12.4ft^3;
//Eps=0.01ft;

double L = 10, r = 1, V = 12.4;//题给条件
double Eps = 0.01;//题目要求的误差

//print函数主要实现题目中要求零点的误差在Eps=0.01ft之内的判断,并输出对应结果
void print(double x, double (*f)(double)){
    if(f(x - Eps) * f(x + Eps) < 0){
        CheckPrint(x, f);
        cout << "水的深度近似为:" << r - x << endl;
    }
    else{
        cout << "计算得到水的深度误差在" << Eps << "ft之外,请调整误差!" << endl;
    }
}

double f(double h){
    return V - L*(0.5 * M_PI * r * r - r * r * asin(h / r) - h * sqrt(r * r - h * h));
}

double df(double h){
    return 2 * L * sqrt(r * r - h * h);
}

int main(){

    double eps = 10e-7;

    //1.二分法
    Bisection method_1(0 , 0.5, eps, f);
    cout << "二分法:";
    print(method_1.solve(), f);

    //2.牛顿法
    Newton method_2(0.1, eps, f, df);
    cout << "牛顿法:";
    print(method_2.solve(), f);

    //3.割线法
    Secant method_3(0.1, 0.2, eps, f);
    cout << "割线法:";
    print(method_3.solve(), f);

    return 0;
}